﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Project.Engine.Utilities
{
    public class StringUtil
    {
        #region 
        private static DefineUtil define = new DefineUtil();

        private const string uniChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTVWXYZàáảãạâầấẩẫậăằắẳẵặèéẻẽẹêềếểễệđìíỉĩịòóỏõọôồốổỗộơờớởỡợùúủũụưừứửữựỳýỷỹỵÀÁẢÃẠÂẦẤẨẪẬĂẰẮẲẴẶÈÉẺẼẸÊỀẾỂỄỆĐÌÍỈĨỊÒÓỎÕỌÔỒỐỔỖỘƠỜỚỞỠỢÙÚỦŨỤƯỪỨỬỮỰỲÝỶỸỴÂĂĐÔƠƯ1234567890~!@#$%^&*()_+|?[]{},./\\=- ";

        private const string KoDauChars =            "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTVWXYZaaaaaaaaaaaaaaaaaeeeeeeeeeeediiiiiooooooooooooooooouuuuuuuuuuuyyyyyAAAAAAAAAAAAAAAAAEEEEEEEEEEEDIIIOOOOOOOOOOOOOOOOOOOUUUUUUUUUUUYYYYYAADOOU1234567890~!@#$%^&*()_+|?[]{},./\\=- ";

        private const string Alphabe = "qwertyuioplkjhgfdsazxcvbnm0123456789";
        #endregion

        public static string UnicodeToKoDau(string s)
        {
            return UnicodeToKoDau(s, false);
        }

        public static string UnicodeToKoDau(string s, bool onlyunicode)
        {
            string retVal = String.Empty;
            if (s == null)
                return retVal;
            int pos;
            for (int i = 0; i < s.Length; i++)
            {
                pos = uniChars.IndexOf(s[i].ToString());
                if (pos >= 0)
                    retVal += KoDauChars[pos];
                else if (!onlyunicode)
                    retVal += s[i];
            }

            return StripHTML(retVal);
        }

        public static string StripHTML(string inputString)
        {
            if (String.IsNullOrEmpty(inputString))
                return String.Empty;

            string retval = String.Empty;
            retval = Regex.Replace(inputString, define.HTML_TAG_PATTERN, String.Empty);
            retval = retval.Replace("\"", "");
            retval = retval.Replace("'", "");

            return retval;
        }

        public static string UnicodeToKoDauUrl(string s)
        {
            s = UnicodeToKoDau(s).Trim().ToLower().Replace("  ", " ");
            s = s.Replace("...", "");
            s = s.Replace("!", "");
            s = s.Replace("<", "");
            s = s.Replace(">", "");
            s = s.Replace("?", "");
            s = s.Replace("@", "");
            s = s.Replace("#", "");
            s = s.Replace("$", "");
            s = s.Replace("%", "");
            s = s.Replace("*", "");
            s = s.Replace("&", "");
            s = s.Replace(":", "");

            s = new System.Text.RegularExpressions.Regex("<[^>]*>", System.Text.RegularExpressions.RegexOptions.None).Replace(s, "");

            string ss = String.Empty;
            for (int i = 0; i < s.Length; i++)
            {
                if (Alphabe.IndexOf(s[i]) >= 0)
                    ss += s[i];
                else
                    ss += ' ';
            }

            //s = UnicodeToKoDau(s).Trim().ToLower().Replace("  ", " ");
            ss = ss.Replace(' ', '-');

            while (ss.IndexOf("--") >= 0)
                ss = ss.Replace("--", "-");

            return ss.Length > 100 ? ss.Substring(0, 100) : ss;
        }

        public static string ShortContent(string content, int numOfWord)
        {
            string retval = content;

            string[] temp = content.Split(' ');

            if (temp.Length > numOfWord)
            {
                retval = string.Empty;
                for (int i = 0; i < numOfWord; i++)
                {
                    retval += temp[i] + " ";
                }
                retval = retval.Substring(0, retval.Length - 1);
                retval += "...";
            }

            return retval;
        }

        public static string UnicodeToISO8859(string src)
        {
            Encoding iso = Encoding.GetEncoding("iso8859-1");
            Encoding unicode = Encoding.UTF8;
            byte[] unicodeBytes = unicode.GetBytes(src);
            return iso.GetString(unicodeBytes);
        }

        public static string ISO8859ToUnicode(string src)
        {
            Encoding iso = Encoding.GetEncoding("iso8859-1");
            Encoding unicode = Encoding.UTF8;
            byte[] isoBytes = iso.GetBytes(src);
            return unicode.GetString(isoBytes);
        }
    }
}
